import store from '@/store'
import { Message } from 'element-ui'


// 退出登陆 ok
function deviceExitNoticeHandle(msg) {
    // Message.success('退出登陆成功！')
}

// 定时任务列表 ok
function getTaskInfoListRespHandle(msg) {
    if (msg.message) {
        let messages = JSON.parse(msg.message)
        store.commit('nedb/SET_GROUP_SEND_LOG', messages)
    }
}

// 批量加好友返回 ok
function autoFriendAddTaskRespHandle(msg) {
    if (msg.message === 'success') {
        Message({
            type: 'success',
            message: '创建批量加好友任务成功！',
            duration: 1000
        })
    } else {
        Message({
            type: 'warning',
            message: '创建批量加好友任务失败！',
            duration: 2000
        })
    }
}

// 获取批量加好友日志返回 ok
function getAutoFriendAddTaskListRespHandle(msg) {
    if (msg.message) {
        let messages = JSON.parse(msg.message)
        store.commit('nedb/SET_AUTO_ADD_FRIEND_LOG', messages)
    } else {
        Message({
            type: 'info',
            message: '数据库没有批量加好友的数据！',
            duration: 1000
        })
    }
}

// 获取号码列表返回 ok
function getPhoneNumberListResp(msg) {
    if (msg.message) {
        let messages = JSON.parse(msg.message)
        store.commit('nedb/SET_PHONES_LIST', messages)
    } else {
        Message({
            type: 'info',
            message: '数据库没有号码！',
            duration: 1000
        })
    }
}

/**
 * 从服务端获取历史消息
 * @param {*} msg
 */
function queryHistoryMessageRespHandle(msg) {
    let data = msg.message ? JSON.parse(msg.message) : {}
    if (data.resultList && data.resultList.length > 0) {
        let messages = data.resultList
        // let wechatId = messages[0].wechatId
        // let friendId = messages[0].friendId
        // let userId = store.getters.userId
        // nedb.chats.find(
        //   {
        //     userId: userId,
        //     WeChatId: wechatId,
        //     FriendId: friendId
        //   },
        //   (err, docs) => {
        //     if (err) return
        //     let alreadyExist = [] // 已经存在的会话
        //     let insertChats = [] // 需要insert的会话
        //     for (const doc of docs) {
        //       alreadyExist.push(doc.msgSvrId)
        //     }

        //     for (const chat of messages) {
        //       if (alreadyExist.indexOf(chat.msgSvrId) < 0) {
        //         insertChats.push(chat)
        //       }
        //     }

        //     if (insertChats.length === 0) return
        //     let chats = []
        //     for (const chat of insertChats) {
        //       let nc = {}
        //       nc.Content = chat.content ? base64.b64EncodeUnicode(chat.content) : ''
        //       nc.ContentType = chat.contentType
        //       nc.IsSend = chat.isSend === 'true' ? 1 : 0
        //       nc.MsgId = chat.msgId ? chat.msgId : ''
        //       nc.msgSvrId = chat.msgSvrId
        //       nc.createTime = chat.create_time
        //       nc.FriendId = chat.friendId
        //       nc.WeChatId = chat.wechatId
        //       nc.userId = userId
        //       chats.push(nc)
        //     }
        //     nedb.chats.insert(chats, (err) => {
        //       if (err) return
        //       store.dispatch('conversation/SetCurrentChats')
        //     })
        //   }
        // )
    }
}

/**
 * 从服务端获取历史消息
 * @param {*} msg
 */
function historyMessageListRespHandle(msg) {
    let data = msg.message ? JSON.parse(msg.message) : {}
    if (data.resultList && data.resultList.length > 0) {
        let messages = data.resultList
        let chats = []
        // let userId = store.getters.userId
        // for (const chat of messages) {
        //     let nc = {}
        //     nc.Content = chat.content ? base64.b64EncodeUnicode(chat.content) : ''
        //     nc.ContentType = chat.contentType
        //     nc.IsSend = chat.isSend ? 1 : 0
        //     nc.MsgId = chat.wechatId ? chat.wechatId : ''
        //     nc.msgSvrId = chat.msgSvrId
        //     nc.createTime = chat.create_time
        //     nc.FriendId = chat.friendId
        //     nc.WeChatId = chat.wechatId
        //     nc.userId = userId
        //     chats.push(nc)
        // }
        // nedb.chats.insert(chats, (err, newDocs) => {
        //   store.dispatch('conversation/SetCurrentChats')
        // })

    }
}

// 公共术语 ok
function getCommonTermsRespHandle(msg) {
    if (msg.message) {
        let message = JSON.parse(msg.message)
        Message({
            type: 'success',
            message: '获取公共术语成功！',
            duration: 1000
        })
        store.commit('conversation/SET_PUBLIC_REPLY', message)
    } else {
        Message({
            type: 'warning',
            message: '服务端没有设置公共术语！',
            duration: 2000
        })
    }
}

// 任务列表
function getTodoHandle(msg){
    if (msg.message) {
        let message = JSON.parse(msg.message)
        Message({
            type: 'success',
            message: '获取任务列表成功！',
            duration: 1000
        })
        store.commit('conversation/GET_TODU_LIST', message)
    } else {
        Message({
            type: 'warning',
            message: '没有任务列表！',
            duration: 2000
        })
    }
}
function getTaskDetailManager(msg){
    if (msg.message) {
        let message = JSON.parse(msg.message)
        Message({
            type: 'success',
            message: '获取任务明细列表成功！',
            duration: 1000
        })
        store.commit('conversation/SET_TASKDETAILMANAGER', message)
    } else {
        Message({
            type: 'warning',
            message: '没有任务列表！',
            duration: 2000
        })
    }
}
// 任务回传
function createdatilHandle(msg){
     if (msg.message) {
        let message = JSON.parse(msg.message)
        // console.log(Object.assign(store.state.conversation.inputform,msg.message))
        console.log(message)
        store.commit('conversation/SET_INPUTFORM',message);
        store.commit('conversation/SET_DASK_DETAIL', message)
        store.commit('conversation/SET_ISINPUT', true)
    } else {
        Message({
            type: 'warning',
            message: '没有该任务！',
            duration: 2000
        })
    }

}
// 已完成待办
function checkendHandle(msg){
    console.log(msg)
    if (msg.message) {
        Message({
            type: 'success',
            message: '提交成功！',
            duration: 2000
        })
        store.dispatch('websocket/GetTodoList')
    }else{
        Message({
            type: 'warning',
            message: '提交失败，请联系管理员！',
            duration: 2000
        })
    }
}
// 保存录入
function savetask(msg){
    if (msg.message) {
        let message = JSON.parse(msg.message)
        store.commit('conversation/SET_RELOADTODO', true);
        Message({
            type: 'warning',
            message: '录入成功！',
            duration: 2000
        })
    } else {
        Message({
            type: 'warning',
            message: '录入失败！',
            duration: 2000
        })
    }
}
// 撤回任务详情
function closetaskdetail(msg){
    if (msg.message) {
        Message({
            type: 'warning',
            message: '撤回成功！',
            duration: 2000
        })
    } else {
        Message({
            type: 'warning',
            message: '撤回失败！',
            duration: 2000
        })
    }
}
// 修改录入
function updatatask(msg){
    if (msg.message) {
        let message = JSON.parse(msg.message)
        store.commit('conversation/SET_RELOADTODO', true);
        Message({
            type: 'warning',
            message: '录入成功！',
            duration: 2000
        })
    } else {
        Message({
            type: 'warning',
            message: '录入失败！',
            duration: 2000
        })
    }
}
// 字典列表
function getdictlist(msg){
    if (msg.message) {
        let message = JSON.parse(msg.message)
        store.commit('conversation/SET_DICT_LIST', message)
    } else {
        Message({
            type: 'warning',
            message: '没有查到字典列表！',
            duration: 2000
        })
    }
}
// 通知列表
function noticelHandle(msg){
    if (msg.message) {
        let message = JSON.parse(msg.message)
        store.commit('conversation/SET_NOTI_LIST', message)
    } else {
        Message({
            type: 'warning',
            message: '没有最新消息！',
            duration: 2000
        })
    }
}
// 一键已读
function closeNoticelHandle(msg){
    if (msg.message) {
        let message = JSON.parse(msg.message)
        // store.commit('conversation/SET_NOTI_LIST', message)
        this.$store.dispatch('websocket/GetNoticeList')
    }
}

// 公共标签 ok
function getCommonTagsRespHandle(msg) {
    if (msg.message) {
        let message = JSON.parse(msg.message)
        store.commit('conversation/SET_PUBLIC_TAGS', message)
    } else {
        Message({
            type: 'warning',
            message: '服务端没有设置公共标签！',
            duration: 2000
        })
    }
}

// =======================================================================
/**
 * 商务列表
 */
function getBuinessListRespHandle(msg) {
    // let message = JSON.parse(msg.message)
    // store.dispatch('chats/SetBusinessList', message)
}



/**
 * 上传好友的备注
 * @param {*} msg
 */
function saveFriendTagDetailRespHandle(msg) {
    let message = JSON.parse(msg.message)
    // store.dispatch('record/SetCurrentRecord', message)
    // let db = store.getters.db
    // websql.updateFriendsLocal(db, message)
}

/**
 * 指定好友的备注
 */
function getWechatFriendDetailRespHandle(msg) {
    let friendInfo = JSON.parse(msg.message)
    // store.dispatch('record/SetCurrentRecord', friendInfo)
    // let db = store.getters.db
    // websql.updateFriendsLocal(db, friendInfo)
}

export default {
    getBuinessListRespHandle,
    getTaskInfoListRespHandle,
    autoFriendAddTaskRespHandle,
    getAutoFriendAddTaskListRespHandle,
    getPhoneNumberListResp,
    getCommonTermsRespHandle,
    saveFriendTagDetailRespHandle,
    getWechatFriendDetailRespHandle,
    getCommonTagsRespHandle,
    deviceExitNoticeHandle,
    historyMessageListRespHandle,
    queryHistoryMessageRespHandle,
    getTodoHandle,
    createdatilHandle,
    getdictlist,
    savetask,
    noticelHandle,
    closeNoticelHandle,
    checkendHandle,
    getTaskDetailManager,
    updatatask,
    closetaskdetail,
}
